package com.softlab.retailwholesalesystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softlab.retailwholesalesystem.entity.OrderEntity;
import com.softlab.retailwholesalesystem.entity.PurchaseEntity;
import com.softlab.retailwholesalesystem.service.IPurchaseService;
import com.softlab.retailwholesalesystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/purchases")
public class PurchaseController {
    @Autowired
    private IPurchaseService service;

    @GetMapping
    public JsonResult<List<PurchaseEntity>> getAll(){
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<PurchaseEntity> purchaseEntities = service.list(wrapper);
        return new JsonResult<>(200,"采购单列表查询成功", purchaseEntities);
    }

    @PostMapping
    public JsonResult<Void> addPurchase(@RequestBody PurchaseEntity purchase){
        service.save(purchase);
        return new JsonResult<>(200, "采购单增加成功");
    }


    @PutMapping
    public JsonResult<Void> changePurchase(@RequestBody PurchaseEntity purchase){
        boolean b = service.updateById(purchase);
        if (!b) return new JsonResult<>(404,"修改失败");
        if (purchase.getStatus().equals("审核通过"))
            service.confirm(purchase);
        return new JsonResult<>(200, "采购单修改成功");
    }

    @DeleteMapping("{id}")
    public JsonResult<Void> deletePurchase(@PathVariable Long id){
        PurchaseEntity purchase=new PurchaseEntity();
        purchase.setPurchaseId(id);
        purchase.setIsDelete(1);
        service.updateById(purchase);
        return new JsonResult<>(200, "采购单删除成功");
    }

    @GetMapping("/search")
    public JsonResult<List<PurchaseEntity>> getPurchaseById(PurchaseEntity purchase){
        List<PurchaseEntity> list = service.searchPurchases(purchase);
        return new JsonResult<>(200, "采购单查找成功", list);
    }
}
